<template>
  <div>
    <el-row>
      <el-col :span="7" style="max-height: 200px" >
        <div style="padding: 10px;margin: 20px " >
          <span>菜单列表</span>
          <el-divider></el-divider>
          <div style="display: flex;align-items: center;">
            <el-input v-model="orgDto.name" placeholder="组织"></el-input>
            <div style="flex: 1; margin: 0 10px;"></div>
            <el-button type="primary" @click="page">搜索</el-button>
          </div >
          <div style="max-height:600px; overflow-y: auto; margin: 20px 0;">
            <el-tree
              class="tree-border"
              :data="orglist"
              show-checkbox
              default-expand-all
              ref="dept"
              empty-text="加载中，请稍候"
              :props="defaultProps"
              @node-click="handleNodeClick"
            ></el-tree>
          </div>
        </div>
      </el-col>

      <el-col :span="17">
       <div style="margin-top: 20px;height: 60px"><h1>新增</h1></div>
        <hr/>
        <div style="margin-top: 30px;">
          <el-form style="margin-left: 100px;" v-model="form">
            <el-row>
              <el-col :span="12">
                <el-form-item label="机构名称" label-width="100px" style="width: 835px">
                  <el-input type="text" v-model="agency_name"></el-input>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="任务名称" label-width="100px" style="width: 835px">
                  <el-input type="text" v-model="form.bean_name" disabled="disabled"></el-input>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="CRON表达式" label-width="100px" style="width: 835px">
                  <el-input type="text" v-model="form.cron_expression"></el-input>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="参数" label-width="100px" style="width: 835px">
                  <el-input type="text" v-model="form.params"></el-input>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="状态" label-width="100px" style="width: 835px">
                  <el-input type="text" v-model="form.status"></el-input>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="备注" label-width="100px" style="width: 835px">
                  <el-input type="text" v-model="form.remark"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>

          <div style="text-align: center">
            <el-button type="primary" v-if="updateButton" @click="updateJob()" style="margin-left: 30px;margin-top: 30px">编辑</el-button>
            <el-button type="primary" v-if="saveButton" @click="createJob()" style="margin-left: 30px;margin-top: 30px">保存</el-button>
            <el-button type="primary" v-if="executeButton" @click="executeJob()" style="margin-left: 30px;margin-top: 30px">立即执行</el-button>
            <el-button type="primary" v-if="suspendButton" @click="suspendJob()" style="margin-left: 30px;margin-top: 30px">暂停任务</el-button>
          </div>

        </div>
      </el-col>
    </el-row>
  </div>

</template>

<script>
import { selectOrg } from '@/api/scheduling/schedulingapi'
import user from '@//store/modules/user'
import { executeScheduleJob,stopScheduleJob,updateScheduleJob,createScheduleJob,getScheduleJobBybusinessId } from '@/api/scheduling/schedulingapi'

export default {
  data(){
    return{
      updateButton:false,
      saveButton:false,
      executeButton:false,
      suspendButton:false,
      orgDto:{
        name:'',
      },
      orglist:[],
      defaultProps: {
        children: 'list',
          label: 'name'
      },
      form:{
        id:null,
        bean_name:null,
        params:null,
        cron_expression:null,
        status:null,
        remark:null,
        business_id:null,
        creator:null,
        updater:null
      },
      agency_name:null,
      orgId:null,
      data:{
        id:null,
        name:null
      }
    }

  },

  //创建生命周期
  created() {
    this.page()
    this.orgId = user.state.orgId
  },


  methods:{
    page(){
      selectOrg().then(as=>{
        this.orglist=as

      })
    },

    //点击组织
    //点击菜单
    handleNodeClick(data){
      this.data = data
      this.clearForm()
      this.orgId = data.id
      this.agency_name=data.name
      this.form.business_id = data.id
      getScheduleJobBybusinessId(data.id).then(response=>{
        if (response.data!==null){
          this.form = response.data
          if (this.form.bean_name !== null){
            this.executeButton = true
            this.suspendButton = true
            this.updateButton = true
            this.saveButton = false
          }
        }else if (this.form.bean_name === null){
          this.executeButton = false
          this.suspendButton = false
          this.updateButton = false
          this.saveButton = true
        }

        this.form.bean_name = 'dispatchTask'
      })
    },
    clearForm(){
      this.form={
        id:null,
          bean_name:null,
          params:null,
          cron_expression:null,
          status:null,
          remark:null,
          business_id:null,
          creator:null,
          updater:null
      }
    },
    //创建任务
    createJob(){
      this.form.business_id = this.orgId
      this.form.creator = user.state.id
      console.log(this.form)
      createScheduleJob(this.form).then(response=>{
        if (response.code === "200") {
          this.$message({
            message: response.message,
            type: 'success'
          });
        } else {
          this.$message.error(response.message);
        }
        this.agency_name = this.data.name
        this.handleNodeClick(this.data);
      })
    },
    //立即执行任务
    executeJob(){
      executeScheduleJob(this.form).then(response=>{
        if (response.code === "200") {
          this.$message({
            message: response.message,
            type: 'success'
          });
        } else {
          this.$message.error(response.message);
        }
        this.agency_name = this.data.name
        this.handleNodeClick(this.data);
      })
    },
    //暂停任务
    suspendJob(){
      this.form.status=0
      stopScheduleJob(this.form).then(response=>{
        if (response.code === "200") {
          this.$message({
            message: response.message,
            type: 'success'
          });
        } else {
          this.$message.error(response.message);
        }
        this.agency_name = this.data.name
        this.handleNodeClick(this.data);
      })
    },
    //修改任务
    updateJob(){
      this.form.updater = user.state.id
      updateScheduleJob(this.form).then(response=>{
        if (response.code === "200") {
          this.$message({
            message: response.message,
            type: 'success'
          });
        } else {
          this.$message.error(response.message);
        }
        this.agency_name = this.data.name
        this.handleNodeClick(this.data);
      })
    }
  }
}
</script>


<style scoped lang="scss">

</style>
